# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
from bookcrawl.items import *
from bookcrawl.spiders.bookschina import BookschinaSpider
from bookcrawl.spiders.dangdang import DangdangSpider
from bookcrawl.spiders.amazon import AmazonSpider
class BookcrawlPipeline(object):
    def __init__(self):
        client = pymongo.MongoClient("211.87.227.243", 27017)
        # client = pymongo.MongoClient()
        db = client.get_database("bookcrawl")
        self.books = db.get_collection("books")
        self.dang_links = db.get_collection("dang_links")
        self.amazon_links = db.get_collection("amazon_links")
        self.bc_links = db.get_collection("bookschina_links")


    def process_item(self, item, spider):

        if isinstance(item,BookItem):
            isbn = item.get("isbn",None)
            if isbn is not None:
                self.books.update_many({"link":item["link"]}, {"$set": dict(item)}, True)
        elif isinstance(item,LinkItem):
            if isinstance(spider,DangdangSpider):
                self.dang_links.update_one({"link_pid":item["link_pid"],"pid":item["pid"]},{"$set":dict(item)},True)
            elif isinstance(spider,BookschinaSpider):
                self.bc_links.update_one({"link_pid":item["link_pid"],"pid":item["pid"]},{"$set":dict(item)},True)
            elif isinstance(spider,AmazonSpider):
                self.amazon_links.update_one({"link_pid":item["link_pid"],"pid":item["pid"]},{"$set":dict(item)},True)
        return item